#pragma strict

public var water : Transform;
public var buoyancy : float = 10;
public var buoyancyCentreOffset : Vector3;

function FixedUpdate ()
{
	var bodyloc : Vector3 = transform.position + transform.TransformDirection(buoyancyCentreOffset);
	if(water.position.y > transform.position.y + 6)
	{
		var forceFactor1 : float = 1f - ((bodyloc.y - water.position.y) / 2);
		var uplift1 : Vector3 = -Physics.gravity * (forceFactor1 - rigidbody.velocity.y * buoyancy);
		rigidbody.AddForce(uplift1);
		return;
	}

	if(water.position.y > transform.position.y + 1)
	{
		var mesh : Mesh = water.GetComponent(MeshFilter).mesh;
		var vertices : Vector3[] = mesh.vertices;

		for(var i = 0; i < vertices.Length; i++)
			{
				var waterloc : Vector3 = water.TransformPoint(vertices[i]);
				var dist : float = Vector3.Distance(waterloc, bodyloc);
				var forceFactor : float = 1f - ((bodyloc.y - waterloc.y) / 2);
				var uplift : Vector3 = -Physics.gravity * (forceFactor - rigidbody.velocity.y * buoyancy);
			
				if(dist > 8)
				{
					continue;
				}
				if(bodyloc.y < waterloc.y);
					rigidbody.AddForceAtPosition(uplift, bodyloc);
			}
	}
}